package cn.bdqn.hospital.dao.impl;

import cn.bdqn.hospital.dao.DrugClassDao;
import cn.bdqn.hospital.entity.DrugClass;
import cn.bdqn.hospital.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class DrugClassDaoImpl implements DrugClassDao {
    QueryRunner queryRunner = new QueryRunner();
    /**
     * 查询所有分类的药品信息
     *
     * @return
     * @throws SQLException
     */
    @Override
    public List<DrugClass> getAll() throws SQLException {
        List<DrugClass> drugClassList =null;
        String sql="select * from drugclass;";
        drugClassList =queryRunner.query(DruidUtils.getConnection(),sql,new BeanListHandler<DrugClass>(DrugClass.class));
        return drugClassList;
    }

    /**
     * 通过名称查询 中药 西药 特殊药
     *
     * @return
     * @throws SQLException
     */
    @Override
    public DrugClass getDrugClassByclassName(String className) throws SQLException {
        DrugClass drugClass=null;
        String sql="select * from drugclass where className=?;";
        drugClass=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<DrugClass>(DrugClass.class),className);
        return drugClass;
    }

    /**
     * 通过分类id查询
     * @param drugClassId
     * @return
     * @throws SQLException
     */
    public DrugClass getDrugClassByDrugclassId(Integer drugClassId) throws SQLException {
        DrugClass drugClass=null;
        String sql="select drugClassN,drugClassId from drugclass where drugClassId=?;";
        drugClass=queryRunner.query(DruidUtils.getConnection(),sql,new BeanHandler<DrugClass>(DrugClass.class),drugClassId);
        return drugClass;
    }
}
